// components/setText/index.js
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        show: {
            type: Boolean,
            default: false
        },
        imgs: {
            type: Array,
            default: []
        },
        maxLength: {
            type: Number,
            default: 1
        },
        tips: {
            type: String,
            default: '建议尺寸（600px * 600px）'
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        newImgs: ''
    },
    observers: {
        'show': function(newVal){
            this.setData({
                newImgs: this.data.imgs
            })
        },
        
    },
    /**
     * 组件的方法列表
     */
    methods: {
        //上传图片
        uploadImg: function () {
            let _this = this;
            let {newImgs,maxLength} = this.data;
            wx.$utils.uploadImg({count: maxLength-newImgs.length},function(url) {
                newImgs.push(url);
                _this.setData({
                    newImgs: newImgs
                })
            })
        },
        //预览图片
        reviewImg: function (e) {
            let {newImgs} = this.data;
            let index = e.currentTarget.dataset.index;
            let current = newImgs[index];
            let urls = newImgs;
            wx.previewImage({
                current: current,
                urls: urls
            })
        },
        //删除图片
        removeImg: function (e) {
            let _this = this;
            let {newImgs} = this.data;
            let index = e.currentTarget.dataset.index;
            wx.showModal({
                title: '提示',
                content: '确定要删除吗',
                success: function (res) {
                    if (res.confirm) {
                        newImgs.splice(index,1);
                        _this.setData({
                            newImgs: newImgs
                        })
                    }
                }
            })
        },
        //关闭
        close: function() {
            this.triggerEvent('confirm');  
        },
        //保存
        saveText: function() {
            let {newImgs} = this.data;
            if(!newImgs.length){
                wx.showToast({
                    title: '请上传图片',
                    icon: 'none'
                })
                return;
            }
            this.triggerEvent('confirm',{change: true, imgs: newImgs});  
        }
    }
})